package com.jxc.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jxc.dao.BaseDaoI; 
import com.jxc.model.po.Serial;
import com.jxc.model.vo.VSerial;
import com.jxc.service.SerialService;

@Service("serialService")
public class SerialServiceImpl implements SerialService {
	@Autowired
	private BaseDaoI<Serial> serialDao;
	
	/* (non-Javadoc)
	 * @see com.jxc.service.impl.SerialService#getAll(com.jxc.model.vo.VSerial)
	 */
	public String getAll(VSerial vs){
		List<Serial> list = new ArrayList<Serial>();
		try {
			String sql = " select t from Serial t where t.rid= ? and t.type = ? ";
			List<Object> values = new ArrayList<Object>();
			values.add(vs.getRid());
			values.add(vs.getType());
			list = serialDao.find(sql, values);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String da = sdf.format(new Date());
			if(list.size()>0){
				Serial se =  list.get(0); 
				Long id = se.getId();
				String od = sdf.format(se.getDate());//旧的时间
				String nd = sdf.format(new Date());//新的时间
				if(Integer.parseInt(od)<Integer.parseInt(nd)){
					vs.setStartid(1);
				}else{
					vs.setStartid(se.getStartid()+1);
				}
				vs.setDate(sdf.parse(da));
				vs.setId(id);
				this.update(vs);
				String tyid = "" ;
				if(vs.getRid()<1000){
					tyid =  "000".substring(0, 3 - vs.getRid().toString().length())+vs.getRid();
				}else{
					tyid =  vs.getRid().toString();	
				}
				String stid = "";
				if(vs.getStartid()<10000){
					stid = "00000".substring(0, 5 - vs.getStartid().toString().length())+vs.getStartid();
				}else{
					stid = vs.getStartid().toString();
				}
				String sign = vs.getType()+"-"+tyid+da+stid;
				return sign;
			}else{
				if(vs.getRid()!=null&&vs.getType()!=null){
					vs.setDate(sdf.parse(da));
					vs.setStartid(1);
					this.add(vs);
					String tyid = "" ;
					if(vs.getRid()<1000){
						tyid =  "000".substring(0, 3 - vs.getRid().toString().length())+vs.getRid();
					}else{
						tyid =  vs.getRid().toString();	
					}
					String stid = "";
					if(vs.getStartid()<10000){
						stid = "00000".substring(0, 5 - vs.getStartid().toString().length())+vs.getStartid();
					}else{
						stid = vs.getStartid().toString();
					}
					String sign = vs.getType()+"-"+tyid+da+stid;
					return sign;
				}
		}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see com.jxc.service.impl.SerialService#update(com.jxc.model.vo.VSerial)
	 */
	public void update(VSerial vs){
		try {
			Serial se = serialDao.get(Serial.class, vs.getId());		
			BeanUtils.copyProperties(vs, se, new String[] { "id","rid","type"});
			serialDao.update(se);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/* (non-Javadoc)
	 * @see com.jxc.service.impl.SerialService#add(com.jxc.model.vo.VSerial)
	 */
	public void add(VSerial vs){
		try {
			Serial se = new Serial();		
			BeanUtils.copyProperties(vs, se);
			serialDao.save(se);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
